<?php


namespace App\Util;


final class Time
{
    /**
     * 返回上季度开始和结束的时间戳 如果$d为true ，则返回本季度开始和结束的时间戳
     *
     * @param boolean $d
     * @return array
     */
   public static function season($d = false) : array
    {
        $season = ceil((date('n'))/3);
        if(!$d)  {
            $season = $season - 1;
        }
        $start =  mktime(0, 0, 0,(int)$season*3-3+1,1,(int)date('Y'));
        $end   =  mktime(23,59,59, (int)$season*3 ,(int) date('t',mktime(0, 0 , 0,(int)$season*3,1,(int)date("Y"))),(int)date('Y'));
        return [$start,$end];

    }

    /**
     * 返回昨日开始和结束的时间戳
     *
     * @return array
     */
    public static function yesterday() : array{
        $yesterday = date('d') - 1;
        return [
            mktime(0, 0, 0, date('m'), $yesterday, date('Y')),
            mktime(23, 59, 59, date('m'), $yesterday, date('Y'))
        ];
    }

    /**
     * @param bool $has_time
     *
     * @return false|int|string
     * 返回今天开始时间戳
     */
    public static function __start_today( ){
        return strtotime(date('Y-m-d').'00:00:00');
    }

    /**
     *
     * @return string 返回当前到今天最后一秒的时间差
     * 返回当前到今天最后一秒的时间差
     */
    public static function todayEnd(){
        return mktime(23,59,59) - mktime(date('H'),date('i'),date('s'));
    }


    /**
     * 返回本周开始和结束的时间戳
     *
     * @return array
     */
    public static function week( $has_time = true):array {
        $timestamp = time();
        $a = strtotime("this week Monday", $timestamp);
        $b = strtotime("this week Sunday", $timestamp);
        if( $has_time ){
            return [
                strtotime(date('Y-m-d', $a)),
                strtotime(date('Y-m-d', $b)) + 24 * 3600 - 1
            ];
        }
        return [
            date('Ymd',$a),
            date('Ymd',$b)
        ];

    }

    /**
     * 返回上周开始和结束的时间戳
     *
     * @return array
     */
    public static function lastWeek( $has_time = true) : array {
        $timestamp = time();
        $a = strtotime("last week Monday", $timestamp);
        $b = strtotime("last week Sunday", $timestamp);
        if($has_time){
            return [
                strtotime(date('Y-m-d', $a)),
                strtotime(date('Y-m-d', $b)) + 24 * 3600 - 1
            ];
        }
        return [
            date('Ymd',$a),
            date('Ymd',$b)
        ];

    }

    /**
     * 返回本月开始和结束的时间戳
     *
     * @return array
     */
    public static function month():array{
        return [
            mktime(0, 0, 0, date('m'), 1, date('Y')),
            mktime(23, 59, 59, date('m'), date('t'), date('Y'))
        ];
    }

    /**
     * 返回上个月开始和结束的时间戳
     *
     * @return array
     */
    public static function lastMonth() :array{
        $begin = mktime(0, 0, 0, date('m') - 1, 1, date('Y'));
        $end = mktime(23, 59, 59, date('m') - 1, date('t', $begin), date('Y'));

        return [$begin, $end];
    }

    /**
     * 返回今年开始和结束的时间戳
     *
     * @return array
     */
    public static function year():array {
        return [
            mktime(0, 0, 0, 1, 1, date('Y')),
            mktime(23, 59, 59, 12, 31, date('Y'))
        ];
    }

    /**
     * 返回去年开始和结束的时间戳
     *
     * @return array
     */
    public static function lastYear():array{
        $year = date('Y') - 1;
        return [
            mktime(0, 0, 0, 1, 1, $year),
            mktime(23, 59, 59, 12, 31, $year)
        ];
    }

    /**
     * @param $some
     * @return false|int
     *
     */
    public  static function getSomeDay($some){
        $time = time();
        $some_day = $time - 60 * 60 * 24 * $some;
        $btime = date('Y-m-d' . ' 00:00:00', $some_day);
        $some_day = strtotime($btime);
        return $some_day;
    }
}